package 字符串;

/**
 * @Author Weton Li
 * @Date 2021/4/7 21:10
 */
public class 验证回文串2 {

    public static void main(String[] args) {
        String input = "abaca";
        String input2 = "eeccccbebaeeabebccceea";
        System.out.println(validPalindrome(input2));
        System.out.println(volid(input2));
    }

    public static boolean validPalindrome(String s) {
        int low = 0, high = s.length() - 1;
        while (low < high) {
            char c1 = s.charAt(low), c2 = s.charAt(high);
            if (c1 == c2) {
                ++low;
                --high;
            } else {
                return validPalindrome(s, low, high - 1) || validPalindrome(s, low + 1, high);
            }
        }
        return true;
    }

    public static boolean validPalindrome(String s, int low, int high) {
        for (int i = low, j = high; i < j; ++i, --j) {
            char c1 = s.charAt(i), c2 = s.charAt(j);
            if (c1 != c2) {
                return false;
            }
        }
        return true;
    }

    public static boolean volid(String s){

        int start = 0;
        int end = s.length()-1;

        while (start < end) {

            if (s.charAt(start) == s.charAt(end)) {

                start++;
                end--;
            }else {
                // 判断 start后移一位和end比较，或end前移一位和start比较，若有一个为true，则可以删除跳过的那个元素值，该字符串时回文串
//                boolean case1 = valid1(s,start+1,end);
//                boolean case2 = valid1(s,start,end-1);
//                if (case1==true || case2==true) {
//                    return true;
//                }else {
//                    return false;
//                }
                return (valid1(s,start+1,end)||valid1(s,start,end-1));
            }

        }
        return true;
    }

    private static boolean valid1(String s, int start, int end) {
        int i = start;
        int j = end;
        while (i < j) {

            if (s.charAt(i)!=s.charAt(j)) {
                return false;
            }

            i++;
            j--;
        }
        return true;
    }

}
